package com.techventus.locations;

import android.app.Activity;

import android.content.pm.ActivityInfo;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * The Class LoginCredentials.
 */
public class LoginCredentials extends Activity{
	
	TextView loginTextBox;
	TextView loginPassBox;
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		 setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); 
		setContentView(R.layout.login); // bind the layout to the activity
		 loginTextBox = (TextView)findViewById(R.id.loginnamebox);
		 loginPassBox = (TextView)findViewById(R.id.loginpasswordbox);
		 Button loginButton = (Button)findViewById(R.id.loginconfirmbutton);
		 Button cancelButton = (Button)findViewById(R.id.logincancelbutton);
		loginButton.setOnClickListener(loginClick);
		cancelButton.setOnClickListener(cancelClick);
		Log  .e("TECHVENTUS", "LoginCredentials");
	}
	
	/** The login click. */
	OnClickListener loginClick = new OnClickListener(){

		@Override
		public void onClick(View v) {
			Log.e("TECHVENTUS","ENTERING A TUNNEL");
			SQLiteDatabase sql = openOrCreateDatabase("db",0,null);
			try{
				sql.execSQL("DELETE FROM GOOGLE;");
				Log.e("TECHVENTUS","INSERTING CREDENTIALS");
				if((loginTextBox.getText()).toString().contains("@")){
					sql.execSQL("INSERT INTO GOOGLE (loginName, password) VALUES ('"+loginTextBox.getText()+"','"+loginPassBox.getText().toString()+"');");
					sql.execSQL("INSERT INTO COMMAND (command) VALUES ('updateLoginCredentials');");
					
				} else{
					sql.execSQL("INSERT INTO GOOGLE (loginName, password) VALUES ('"+loginTextBox.getText().toString().replace("@gmail.com","").replace("@googlemail.com", "")+"@gmail.com','"+loginPassBox.getText().toString()+"');");
					sql.execSQL("INSERT INTO COMMAND (command) VALUES ('updateLoginCredentials');");
				}
				System.out.println("login credentials inserted + "+loginTextBox.getText().toString());
			}catch(Exception u){
				u.printStackTrace();
			}

			
			
			/*
			
			if(c.isAfterLast()){

			}else{
				Log.e("TECHVENTUS","NOT AFTer LASt");
				sql.execSQL("UPDATE GOOGLE SET loginName = '"+loginText+"@gmail.com' and password = '"+loginPass+"'");
				System.out.println("login credentials updated");
			}
			//LoginCredentials.this.finish();
			*/
			sql.close();
			LoginCredentials.this.finish();//finalize();
			
		}
		
	};
	
	
	OnClickListener cancelClick = new OnClickListener(){

		@Override
		public void onClick(View v) {
			Log.e("TECHVENTUS","Exiting CREDENTIALS FROM CANCEL");
			LoginCredentials.this.finish();
			
		}
		
	};
	
}
